package com.example.ui.contact.lbs;





import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.util.Util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LBSActivity extends Activity{



    // 7FFF4D409DF9980CDE086E44F0F8076FF4005DB7
	// 定位相关
	LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();
	
	MyLocationOverlay myLocationOverlay = null;
	int index =0;
	LocationData locData = null;
	Button testUpdateButton = null;	
	private double latitude = 0.1;
	private double longitude = 0.2;
	
	Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(LBSActivity.this, "msg:" +msg.what, Toast.LENGTH_SHORT).show();
        };
    };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_lbs_main);
        HSApplication app = (HSApplication)this.getApplication();
        if (app.mBMapManager == null) {
            app.mBMapManager = new BMapManager(this);
            app.mBMapManager.init(HSApplication.mapKey,new HSApplication.MyGeneralListener());
        }
        
        locData = new LocationData();
        mLocClient = new LocationClient( this );
        mLocClient.registerLocationListener( myListener );
        
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);//打开gps
        option.setCoorType("bd09ll");     //设置坐标类型
        option.setScanSpan(10000);
        mLocClient.setLocOption(option);
        mLocClient.start();
        
		testUpdateButton = (Button)findViewById(R.id.button1);
		OnClickListener clickListener = new OnClickListener(){

				@Override
				public void onClick(View v) {
					//testUpdateClick();
					  //LbsOpen();
				}
	        };
	    testUpdateButton.setOnClickListener(clickListener);       
	    LbsOpen();
   
	}
	
	public void LbsOpen(){
		new AlertDialog.Builder(LBSActivity.this).setTitle("提示")
		.setMessage("需要提交自己的位置")
		.setNegativeButton("取消", null)
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				testUpdateClick();
			}
			
		}).show();
	}

    
    @Override
    protected void onPause() {
        super.onPause();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    }
    
    
    @Override
    protected void onDestroy() {
        if (mLocClient != null)
            mLocClient.stop();
        HSApplication app = (HSApplication)this.getApplication();
        if (app.mBMapManager != null) {
            app.mBMapManager.destroy();
            app.mBMapManager = null;
        }
        super.onDestroy();
    }
       

    
    public void testUpdateClick(){
        mLocClient.requestLocation();
    }


	/**
     * 监听函数，又新位置的时候，格式化成字符串，输出到屏幕中
     */
    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null)
                return ;
            
            locData.latitude = location.getLatitude();
            locData.longitude = location.getLongitude();
            locData.accuracy = location.getRadius();
            locData.direction = location.getDerect();
//            myLocationOverlay.setData(locData);
//            Util.Debug("经度：", ""+locData.latitude);
//            Util.Debug("纬度：", ""+locData.longitude);
            latitude = locData.latitude;
            longitude = locData.longitude;
        }
        
        public void onReceivePoi(BDLocation poiLocation) {
            if (poiLocation == null){
                return ;
            }
        }
    }
    
    
   
}